package com.example.myapplication.shortvideo.player;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dueeeke.videoplayer.controller.BaseVideoController;
import com.dueeeke.videoplayer.player.IjkVideoView;
import com.dueeeke.videoplayer.util.L;
import com.example.myapplication.R;
import com.example.myapplication.shortvideo.utils.NetWorkUtils;


public class TikTokController extends BaseVideoController {

    public static boolean volTag = true;// 默认开音
    private ImageView thumb;
    private Dialog lodingDialog;
    private boolean hidden;//是不是切换到别的页面了
    private long countDownTime;
    private Activity mActivity;

    public TikTokController(@NonNull Activity context) {
        super(context);
        mActivity = context;
    }

    public TikTokController(@NonNull Activity context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TikTokController(@NonNull Activity context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_tiktok_controller;
    }

    @Override
    protected void initView() {
        super.initView();
        thumb = mControllerView.findViewById(R.id.iv_thumb);
        lodingDialog = buildSetDialog(thumb.getContext(), true);
        L.setDebug(false);
    }

    @Override
    public void setPlayState(int playState) {
//        super.setPlayState(playState);
        mCurrentPlayState = playState;
        hideStatusView();
        switch (playState) {
            case IjkVideoView.STATE_IDLE:
                L.e("sssss STATE_IDLE");
                thumb.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_PLAYING:
                L.e("sssss STATE_PLAYING");
                thumb.setVisibility(GONE);
                loadingDismiss();
                mHandler.removeMessages(1);
                if (playCallback != null) {
                    playCallback.playing();
                }
                if (hidden) {
                    mMediaPlayer.pause();
                }
                break;
            case IjkVideoView.STATE_BUFFERING:
                L.e("sssss STATE_BUFFERING");
                mHandler.sendEmptyMessageDelayed(1, 2000);
                break;
            case IjkVideoView.STATE_BUFFERED:
                L.e("sssss STATE_BUFFERED");
                loadingDismiss();
                countDownTime = System.currentTimeMillis();
                mHandler.removeMessages(1);
                break;
            case IjkVideoView.STATE_PREPARING:
                L.e("sssss STATE_PREPARING");
                countDownTime = System.currentTimeMillis();
                mHandler.sendEmptyMessageDelayed(1, 2000);
                break;
            case IjkVideoView.STATE_PREPARED:
                loadingDismiss();
                mHandler.removeMessages(1);
                L.e("sssss STATE_PREPARED");
                break;
            case IjkVideoView.STATE_ERROR:
                if (!NetWorkUtils.isNetConnected(mActivity)) {
//                    mStatusView.setMessage("网络异常,请检查网络哦~");
                    playCallback.notNetWork();
                }else {
                    mStatusView.setMessage("视频无法播放~");
                    playCallback.playError();
                    mHandler.removeMessages(1);
                }
//
                mStatusView.setButtonTextAndAction("重试", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hideStatusView();
                        mMediaPlayer.replay(true);
                    }
                });
                this.addView(mStatusView, 0);
                break;
        }
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (System.currentTimeMillis() - countDownTime > 1800) {
                    if (lodingDialog != null && mActivity != null && !mActivity.isFinishing()) {
                        lodingDialog.show();
                    }
                }
            }
        }
    };

    public void loadingDismiss() {
        if (lodingDialog != null) {
            lodingDialog.dismiss();
        }
    }

    public ImageView getThumb() {
        return thumb;
    }

    public void onHiddenChanged(boolean hidden) {
        this.hidden = hidden;
        if (mMediaPlayer != null) {
            if (hidden) {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.pause();
                }
            } else {
                if (!mMediaPlayer.isPlaying()) {
                    mMediaPlayer.start();
                }
            }
        }
    }

    private StartPlayCallback playCallback;

    public void setStartPlayCallback(StartPlayCallback playCallback) {
        this.playCallback = playCallback;
    }


    public interface StartPlayCallback {
        void playing();
        void notNetWork();
        void playError();
    }

    public Dialog buildSetDialog(Context context, boolean isCancel) {
        Dialog dialog = new Dialog(context, R.style.live_loading);
        View view = LayoutInflater.from(context).inflate(R.layout.animation_loding_video, null);
//        ImageView img_show = view.findViewById(R.id.img_show);
//        AnimationDrawable animationDrawable = (AnimationDrawable) img_show.getBackground();
//        animationDrawable.start();
        dialog.setContentView(view);
        dialog.setCancelable(isCancel);
        dialog.setCanceledOnTouchOutside(isCancel);
        return dialog;
    }

    public void destroyLoading() {
        mHandler.removeMessages(1);
        loadingDismiss();
    }



}